package com.ssoclient.test;

import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.base.Stopwatch;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.*;
import com.google.common.io.Files;
import com.google.common.util.concurrent.*;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Slf4j
public class GuavaDemo {
   BlockingDeque b = new LinkedBlockingDeque(10);
   ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("thread-format-%d").build();
    ExecutorService service = new ThreadPoolExecutor(5,
            10,50L,TimeUnit.SECONDS,b,threadFactory,new ThreadPoolExecutor.AbortPolicy());
    
    public static void main(String[] args) throws Exception {

        System.out.println("==========================================================================================");
        //sring转map
        String str = "a=1,b=2,c=3";
        Map<String, String> split = Splitter.on(",").withKeyValueSeparator("=").split(str);
        System.out.println(split);

        System.out.println("==========================================================================================");
        String string = "1-2-3-4-5-6-7-8 9-9";
        List<String> list = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(string);
        System.out.println(list);
        System.out.println("==========================================================================================");
        BiMap<Object, Object> biMap = HashBiMap.create();
        biMap.put("aa","aa");
        biMap.put("b","bb");
        biMap.put("c","cc");
        biMap.forcePut("d","cc");
        System.out.println(biMap);
        BiMap<Object, Object> inverse = biMap.inverse();
        System.out.println(inverse);
        //hashMultiset();
        arrayListMultimap();
        System.out.println("==========================================================================================");
        //去除数字
        String s = CharMatcher.inRange('0', '9').retainFrom("gafda8dafdf89999");
        System.out.println(s);
        //去除字符
        String daf788daf = CharMatcher.inRange('0', '9').removeFrom("daf788daf");
        System.out.println(daf788daf);
        int count = -1;
        String a = null;
        //IllegalArgumentException 异常
       // Preconditions.checkArgument(count > 0,"====%s",count);
        //NullPointerException  异常
       // Preconditions.checkNotNull(a);
        System.out.println("================================计算中间代码运行时间==========================================================");
        Stopwatch started = Stopwatch.createStarted();
        for (int i = 0; i < 10000; i++) {

        }
        long elapsed = started.elapsed(TimeUnit.MICROSECONDS);
        System.out.println(elapsed);
        System.out.println("================================文件操作==========================================================");
        File file = new File("D:\\test/demo.txt");
        List<String> list1 =null;
        try {
            list1 = Files.readLines(file, Charsets.UTF_8);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(list1);
        //文件复制
        Files.copy(file,new File("D:\\test/demo1.txt"));
        //删除文件夹下的内容

        System.out.println("=========================缓存=================================================================");
        LoadingCache<Object, Object> build = CacheBuilder
                .newBuilder()
                .build(new CacheLoader<Object, Object>() {
                    @Override
                    public Object load(Object key) throws Exception {

                        String str = "eilen=" + key + "!";
                        return str;
                    }
                });
        Object ren = build.get("人帅");
        System.out.println(ren);
        build.put("aa","bb");
        System.out.println(build.get("aa"));
        System.out.println("==================================缓存2=========================================================");
        Cache<Object, Object> cache = CacheBuilder.newBuilder().maximumSize(1000).build();
        Object get = cache.get("get", new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "daafdafdfa";
            }
        });
        System.out.println(get);
        System.out.println("=================guava中的并发=========================================================================");
        FutureCallback<Integer> futureCallback =  new FutureCallback<Integer>(){

            //成功之后执行的方法
            @Override
            public void onSuccess(Integer result) {
                log.info("成功之后执行的：{}",result);
                num+=result;
                log.info("num:{}",num);
            }

            //失败之后执行的方法
            @Override
            public void onFailure(Throwable t) {
                System.out.println(t.toString());
            }
        };

        ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
        ListenableFuture<Integer> integerListenableFuture = null;
        for (int i = 0; i < 10; i++) {
            Integer final1 = i;
           integerListenableFuture = listeningExecutorService.submit(()->{
                return Integer.valueOf(final1+1);
            });
            Futures.addCallback(integerListenableFuture,futureCallback);
        }
        listeningExecutorService.shutdownNow();
        log.info("关闭线程池中：{},{}",listeningExecutorService.isShutdown(),listeningExecutorService.isTerminated());
        System.out.println("@@@@@@@@@@"+integerListenableFuture.get());

        System.out.println("==========================================================================================");

    }

private static int num = 0;
    private static void hashMultiset() {
        Multiset<Object> multiset = HashMultiset.create();
        multiset.add("aaa");
        multiset.add("aaa");
        System.out.println(multiset.count("aaa"));
        multiset.stream().forEach(p->{
            System.out.println(p);
        });
    }

    private static void arrayListMultimap() {
        //此方法是一个map中value为list类型
        Multimap<Object, Object> multimap = ArrayListMultimap.create();
        multimap.put("aa","aav");
        multimap.put("aa","aav");
        multimap.put("ccv","ccv");
        System.out.println(multimap.get("aa"));
    }
}
